import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'ecgcontroller.dart';

class WaveSettingDialog extends StatefulWidget {
  WaveSettingDialog({
    Key? key,
    this.maxWidth = double.infinity,
    this.maxHeight = double.infinity,
    this.ecgSelectedXZoom = '25mm/s',
    this.ecgSelectedYZoom = '1.00X',
    this.plethSelectedXZoom = '25mm/s',
    this.plethSelectedYZoom = '1.00X',
    this.respSelectedXZoom = '25mm/s',
    this.respSelectedYZoom = '1.00X',
    required this.controller,
  }) : super(key: key);
  late EcgController controller;
  double maxWidth = double.infinity;
  double maxHeight = double.infinity;
  String ecgSelectedXZoom;
  String ecgSelectedYZoom;
  String plethSelectedXZoom;
  String plethSelectedYZoom;
  String respSelectedXZoom;
  String respSelectedYZoom;

  @override
  _WaveSettingDialogState createState() => _WaveSettingDialogState();
}

class _WaveSettingDialogState extends State<WaveSettingDialog> {
  // ecg
  final List<String> _ecgXZoom = ['6.25mm/s', '12.50mm/s', '25mm/s', '50mm/s'];
  final List<String> _ecgYZoom = [
    '0.125X',
    '0.25X',
    '0.50X',
    '1.00X',
    '2.00X',
    '4.00X',
  ];

  // pleth
  final List<String> _plethXZoom = [
    '6.25mm/s',
    '12.50mm/s',
    '25mm/s',
    '50mm/s'
  ];
  final List<String> _plethYZoom = [
    '0.125X',
    '0.25X',
    '0.50X',
    '1.00X',
    '2.00X',
    '4.00X',
  ];
  final List<String> _respXZoom = ['6.25mm/s', '12.50mm/s', '25mm/s', '50mm/s'];
  //resp
  final List<String> _respYZoom = [
    '0.125X',
    '0.25X',
    '0.50X',
    '1.00X',
    '2.00X',
    '4.00X',
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
          maxHeight: widget.maxHeight, maxWidth: widget.maxWidth),
      decoration: const BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(color: Colors.black12, blurRadius: 20, spreadRadius: 10)
        ],
      ),
      child: SingleChildScrollView(
        child: Column(children: [
          const ListTile(
            leading: Icon(Icons.settings),
            title: Text(
              '心电图波形参数设置',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 60),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text('波形增益：'),
                DropdownButton(
                  value: widget.ecgSelectedYZoom,
                  items: _ecgYZoom
                      .map((code) => DropdownMenuItem(
                          value: code, child: Text(code.toString())))
                      .toList(),
                  onChanged: (val) {
                    widget.ecgSelectedYZoom = val.toString();
                    setState(() {});
                  },
                ),
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 60),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text('波形速度：'),
                DropdownButton(
                    value: widget.ecgSelectedXZoom,
                    items: _ecgXZoom
                        .map((code) => DropdownMenuItem(
                            value: code, child: Text(code.toString())))
                        .toList(),
                    onChanged: (val) {
                      widget.ecgSelectedXZoom = val.toString();
                      setState(() {});
                    }),
              ],
            ),
          ),
          Container(height: 1, color: Colors.black.withOpacity(0.1)),
          const ListTile(
            leading: Icon(Icons.settings),
            title: Text(
              '血氧波形参数设置',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 60),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text('波形速度：'),
                DropdownButton(
                    value: widget.plethSelectedXZoom,
                    items: _plethXZoom
                        .map((code) => DropdownMenuItem(
                            value: code, child: Text(code.toString())))
                        .toList(),
                    onChanged: (val) {
                      widget.plethSelectedXZoom = val.toString();
                      setState(() {});
                    }),
              ],
            ),
          ),
          Container(height: 1, color: Colors.black.withOpacity(0.1)),
          const ListTile(
            leading: Icon(Icons.settings),
            title: Text(
              '呼吸波形参数设置',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 60),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text('波形增益：'),
                DropdownButton(
                    value: widget.respSelectedYZoom,
                    items: _respYZoom
                        .map((code) => DropdownMenuItem(
                            value: code, child: Text(code.toString())))
                        .toList(),
                    onChanged: (val) {
                      widget.respSelectedYZoom = val.toString();
                      setState(() {});
                    }),
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 60),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text('波形速度：'),
                DropdownButton(
                    value: widget.respSelectedXZoom,
                    items: _respXZoom
                        .map((code) => DropdownMenuItem(
                            value: code, child: Text(code.toString())))
                        .toList(),
                    onChanged: (val) {
                      widget.respSelectedXZoom = val.toString();
                      setState(() {});
                    }),
              ],
            ),
          ),
          Container(height: 1, color: Colors.black.withOpacity(0.1)),
          const Divider(height: 20),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () async {
                    await widget.controller.pref
                        .write('ecg_setting_valid', false);
                    await widget.controller.pref
                        .write('pleth_setting_valid', false);
                    await widget.controller.pref
                        .write('resp_setting_valid', false);
                    widget.controller.ecgWaveIContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveIIContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV1Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV2Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV3Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV4Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV5Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV6Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveIIIContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveAVRContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveAVLContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveAVFContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.plethWaveContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'pleth');
                    widget.controller.respWaveContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'resp');
                    SmartDialog.dismiss();
                  },
                  child: const SizedBox(
                      width: 100, child: Center(child: Text('重置')))),
              const SizedBox(width: 20),
              ElevatedButton(
                  onPressed: () async {
                    widget.controller.ecgWaveIContext.value.xZoom =
                        widget.ecgSelectedXZoom;
                    widget.controller.ecgWaveIContext.value.yZoom =
                        widget.ecgSelectedYZoom;
                    await widget.controller.ecgWaveIContext.value.save();
                    widget.controller.plethWaveContext.value.xZoom =
                        widget.plethSelectedXZoom;
                    widget.controller.plethWaveContext.value.yZoom =
                        widget.plethSelectedYZoom;
                    await widget.controller.plethWaveContext.value.save();
                    widget.controller.respWaveContext.value.xZoom =
                        widget.respSelectedXZoom;
                    widget.controller.respWaveContext.value.yZoom =
                        widget.respSelectedYZoom;
                    await widget.controller.respWaveContext.value.save();

                    widget.controller.ecgWaveIContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveIIContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV1Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV2Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV3Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV4Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV5Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveV6Context.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveIIIContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveAVRContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveAVLContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.ecgWaveAVFContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'ecg');
                    widget.controller.plethWaveContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'pleth');
                    widget.controller.respWaveContext.value = WaveParameter(
                        pref: widget.controller.pref, waveType: 'resp');
                    SmartDialog.dismiss();
                  },
                  child: const SizedBox(
                      width: 100, child: Center(child: Text('保存')))),
            ],
          ),
          const Divider(height: 20),
        ]),
      ),
    );
  }
}
